package com.linktrip.devicemanager.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.linktrip.devicemanager.entity.DeviceInfo;

/**
 * @author : CaineZhu E-mail:CaineZhu@me.com
 * @version : V1.0
 * @创建时间 ： 2017年5月2日 下午2:11:09
 * @类说明 : 文件辅助类
 */
public class FileUtil {

	static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

	// 升级文件分割
	public static boolean splitFile(String source, String target, String prefix) {
		File file = new File(source);
		if (!file.exists())
			return false;

		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			int index = 1;
			int len = -1;
			fis = new FileInputStream(file);
			byte[] data = new byte[256];
			while ((len = fis.read(data)) != -1) {

				File newFile = new File(target + prefix + index);
				File dir = new File(target);
				if (!dir.exists())
					dir.mkdirs();
				if (!newFile.exists())
					newFile.createNewFile();
				fos = new FileOutputStream(newFile);
				if (len >= 256) {
					fos.write(data);
				} else {
					for (int i = 0; i < len; i++) // 不足256时 读取实际的byte 防止脏数据
						fos.write(data[i]);
				}

				fos.close();
				index++;
			}
			fis.close();

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public static int splitFile(byte[] source, String target, String prefix) {

		FileOutputStream fos = null;
		int index = 0;
		try {

			int len = -1;
			ByteArrayInputStream bis = new ByteArrayInputStream(source);
			byte[] data = new byte[256];
			while ((len = bis.read(data)) != -1) {
				index++;
				File newFile = new File(target + prefix + index);
				File dir = new File(target);
				if (!dir.exists())
					dir.mkdirs();
				if (!newFile.exists())
					newFile.createNewFile();
				fos = new FileOutputStream(newFile);
				if (len >= 256) {
					fos.write(data);
				} else {
					for (int i = 0; i < len; i++) // 不足256时 读取实际的byte 防止脏数据
						fos.write(data[i]);
				}
				fos.close();

			}
			bis.close();

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

		return index;
	}

	public static void deleteFile(String path) {
		File file = new File(path);
		if (file.isDirectory()) {
			String[] list = file.list();
			File f = null;
			for (int i = 0; i < list.length; i++) {
				f = new File(path, list[i]);
				f.delete();
			}

		} else {
			file.delete();

		}
		logger.debug("deleteFile===>path:{} DONE!", path);
	}

	@SuppressWarnings("resource")
	public static byte[] file2Bytes(String path) {
		logger.warn("path:{}",path);
		File file = new File(path);
		if (!file.exists()){
			logger.error("path:{} not exists!",path);
			return null;
			}
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			byte[] data = new byte[256];
			int len = -1;
			len = fis.read(data);
			if (len < 256) {
				byte[] dt = new byte[len];
				System.arraycopy(data, 0, dt, 0, len);
				return dt;
			} else {
				return data;
			}

		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		for (int i = 0; i < 1000; i++) {
			
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					DeviceInfo info = new DeviceInfo();
					info.setDevice_no("QWERT");
					String param = JSONObject.toJSONString(info);
					String url2 = "http://localhost:8080/base/api/callback";
					String result2 = HttpUtil.httpUrlConnectionForJson(url2, "POST", param, null);
				}
			}).start();

		}
		// System.out.println(result);
	}
}
